﻿using System;
using System.Windows.Input;
using Avalonia.Controls.Primitives;
using Avalonia.Input;

namespace KinonekoSoftware.Foundation.UX.Controls
{
    public class DegreeControl : TemplatedControl
    {

        public static readonly StyledProperty<int> ThumbGapProperty
            = AvaloniaProperty.Register<DegreeControl, int>(nameof(ThumbGap), 2);

        public static readonly StyledProperty<int> ThumbSizeProperty
            = AvaloniaProperty.Register<DegreeControl, int>(nameof(ThumbSize), 16);

        public static readonly StyledProperty<int> ValueProperty
            = AvaloniaProperty.Register<DegreeControl, int>(nameof(Value), 2);

        public static readonly StyledProperty<int> MaximumProperty
            = AvaloniaProperty.Register<DegreeControl, int>(
                                                            nameof(Maximum), 10);

        public static readonly StyledProperty<int> MinimumProperty
            = AvaloniaProperty.Register<DegreeControl, int>(
                                                            nameof(Minimum), 0);

        public static readonly StyledProperty<double> RadiusProperty
            = AvaloniaProperty.Register<DegreeControl, double>(
                                                               nameof(Radius), 0d);

        static DegreeControl()
        {

        }


        public double Radius
        {
            get => GetValue(RadiusProperty);
            set => SetValue(RadiusProperty, value);
        }

        public override void Render(DrawingContext dc)
        {

            if (!IsVisible)
            {
                return;
            }

            //
            //
            dc.DrawRectangle(XamlCore.Transparent, null, new Rect(0, 0, Bounds.Width, Bounds.Height));


            var max  = Math.Clamp(Maximum, 0, 32);
            var max2 = max + 1;
            var min  = Math.Clamp(Minimum, 0, 32);
            var x    = 0d;


            if (min > max)
            {
                (max, min) = (min, max);
            }

            var v    = Math.Clamp(Value, min, max);
            var h    = Bounds.Height;
            var gap  = Math.Clamp(ThumbGap, 2, 20);
            var size = Math.Clamp(ThumbSize, 10, 96);
            var bg   = Background                  ?? new SolidColorBrush(Colors.Gray);
            var fg   = (BorderBrush ?? Foreground) ?? new SolidColorBrush(Colors.Olive);
            var r    = Radius;

            //
            //
            for (var i = 1; i < max2; i++)
            {
                var b = v >= i ? fg : bg;

                //
                //
                dc.DrawRectangle(b, null, new Rect(x, 0, size, h), r, r);

                x += gap + size;

            }

            base.Render(dc);
        }
        
        protected override void OnPointerPressed(PointerPressedEventArgs e)
        {
            var size  = Math.Clamp(ThumbSize, 10, 96) + ThumbGap;
            var p     = Math.Round(e.GetPosition(this).X);
            var index = (int)Math.Round(p / size);

            if (index == 1)
            {
                index = Value == 1 ? 0 : 1;
            }

            Value = index;
            base.OnPointerPressed(e);
        }

        public int Minimum
        {
            get => GetValue(MinimumProperty);
            set => SetValue(MinimumProperty, value);
        }
        public int Maximum
        {
            get => GetValue(MaximumProperty);
            set => SetValue(MaximumProperty, value);
        }
        public int Value
        {
            get => GetValue(ValueProperty);
            set => SetValue(ValueProperty, value);
        }

        public int ThumbSize
        {
            get => GetValue(ThumbSizeProperty);
            set => SetValue(ThumbSizeProperty, value);
        }
        public int ThumbGap
        {
            get => GetValue(ThumbGapProperty);
            set => SetValue(ThumbGapProperty, value);
        }
    }
}